package Com.Ring.TradingMobile;

import java.util.ArrayList;

import Com.Ring.TradingMobile.Lib.Common;
import Com.Ring.TradingMobile.Lib.CompanyListAdapter;
import Com.Ring.TradingMobile.Lib.OrderInfo;
import Com.Ring.TradingMobile.Lib.PreTradeInfo;
import Com.Ring.TradingMobile.Lib.RET_CODE;
import Com.Ring.TradingMobile.Lib.Session;
import Com.Ring.TradingMobile.Service.WebService;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MuaActivity extends Activity {
	TextView lbAccount;
	TextView lbBalance;
	AutoCompleteTextView txtSymbol;
	EditText txtVolume;
	EditText txtPrice;
	EditText txtDup;
	EditText txtPIN;
	TextView lblRef;
	TextView lbCeiling;
	TextView lbFloor;
	Button btnBuy;
	Button btnReset;
	Button btnAccountSelector;
	Button btnLinkBan;
	Button btnTrangThaiLenh;
	Button btnHome;
	Button btnBack;
	Button btnSelectPrice;
	
	float CashBalance;
	float price;
	int volume;
	int dup;
	int selectedSubAccountPossition;
	
	String strSymbol;
	String strVolume;
	String strPrice;
	String strDup;
	String strPIN;
	String conPrice = " ";
	String OrderId = "";
	
	PreTradeInfo preTradeInfo;
	OrderInfo OrderInfo;
	String[] marketStatus;
	String TAG = "MuaActivity";	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mua);		
		
		CashBalance = WebService.GetCashBalance(Session.currentSubAccount, 0);
		if(CashBalance == Float.NaN)
		{
			Log.d("TAG", getResources().getString( R.string.str_Message_NotGetCash));
			//lbMessage.setText(R.string.str_Message_NotGetCash);
			Toast.makeText(MuaActivity.this, R.string.str_Message_NotGetCash, Toast.LENGTH_SHORT).show();
			CheckLogin();
		}			
		lbAccount = (TextView)findViewById(R.id.lbAccount);
		lbBalance = (TextView)findViewById(R.id.lbBalance);
		txtSymbol = (AutoCompleteTextView)findViewById(R.id.txtSymbol);
		txtVolume = (EditText)findViewById(R.id.txtVolume);
		txtPrice = (EditText)findViewById(R.id.txtPrice);
		txtDup = (EditText)findViewById(R.id.txtDup);
		txtPIN = (EditText)findViewById(R.id.txtPIN);
		//lbMessage = (TextView)findViewById(R.id.lbMessage);
		btnBuy = (Button)findViewById(R.id.btnBuy);
		btnReset = (Button)findViewById(R.id.btnReset);
		btnAccountSelector = (Button)findViewById(R.id.btnAccountSelector);
		btnLinkBan = (Button)findViewById(R.id.btnLinkBan);
		btnTrangThaiLenh = (Button)findViewById(R.id.btnTrangThaiLenh);
		btnHome = (Button) findViewById(R.id.btnHome);
		btnBack = (Button) findViewById(R.id.btnBack);
		btnSelectPrice = (Button) findViewById(R.id.btnSelectPrice);
		lbCeiling = (TextView)findViewById(R.id.lbCeiling);
		lbFloor = (TextView)findViewById(R.id.lbFloor);
		lblRef = (TextView)findViewById(R.id.lblRef);
		
		btnHome.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), TradingMobileLoggedActivity.class);
				startActivity(intent);
				//finish();
			}
		});
		
		btnBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {				
				finish();
			}
		});
		
		btnLinkBan.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent ban = new Intent(getBaseContext(), BanActivity.class);
				startActivity(ban);
				//finish();
			}
		});
		
		btnTrangThaiLenh.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent ban = new Intent(getBaseContext(), TrangThaiLenhActivity.class);
				startActivity(ban);
				//finish();
			}
		});
		
		btnSelectPrice.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtPrice.setText(btnSelectPrice.getText());
				String lb_price = btnSelectPrice.getText().equals("ATO")?"ATC":"ATO";
				btnSelectPrice.setText(lb_price);
			}
		});
		
		CompanyListAdapter adapter = new CompanyListAdapter(this, TAG, this, R.layout.two_line_list_item, null);
		txtSymbol.setAdapter(adapter);
		txtSymbol.setOnItemClickListener(new OnItemClickListener() {
	            public void onItemClick(AdapterView<?> p, View v, int pos, long id) {
	                strSymbol = txtSymbol.getText().toString();
	                preTradeInfo = WebService.GetPreTradeInfo(Session.currentSubAccount, strSymbol, 0, "B");
					if(preTradeInfo!=null)
					{
						lbCeiling.setText(Float.toString(preTradeInfo.Ceiling));
						lbFloor.setText(Float.toString(preTradeInfo.Floor));
						lblRef.setText(Float.toString(preTradeInfo.RefPrice));
					}
					else
					{
						lbCeiling.setText("...");
						lbFloor.setText("...");
						lblRef.setText("...");
					}
	            }
		});
		
		btnBuy.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				CheckLogin();
				
				Get_Input_Info();			
				
				
				boolean Is_ValidOrder = Check_Order_Info();
				
				if(!Is_ValidOrder)
					return;
				
				if(OrderId.equals(""))
					PutOrder();
				else
					UpdateOrder();
			}						
		});
		
		btnReset.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtSymbol.setText("");
				txtPrice.setText("");
				txtVolume.setText("");
				txtPIN.setText("");
				txtDup.setText("");
				//lbMessage.setText("");
				
				CheckLogin();
			}
		});
					
		btnAccountSelector.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builderSua = new Builder(MuaActivity.this);
				builderSua.setIcon(R.drawable.alert_dialog_icon);
				builderSua.setTitle(R.string.strConfirmSelectSubAccount);

				String[] subAccountList = new String[Session.subCustAccounts.size()];
				for(int i=0; i< subAccountList.length; i++)
					subAccountList[i] = Session.subCustAccounts.get(i);
					
				builderSua.setSingleChoiceItems(subAccountList, Session.currentSubAccountPossition,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								selectedSubAccountPossition = which;																
							}
						});
				builderSua.setPositiveButton(R.string.str_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								if(selectedSubAccountPossition == Session.currentSubAccountPossition)
									return;
								
								//Luu lai thong tin subAccount hien tai
								Session.currentSubAccountPossition = selectedSubAccountPossition;
								Session.currentSubAccount = Session.subCustAccounts.get(selectedSubAccountPossition);
								Load_Current_SubAccount_Info();
							}
						});				
				builderSua.setNegativeButton(R.string.str_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								
							}
						});
				builderSua.show();
			}
		});		
		
		Load_Current_SubAccount_Info();
		
		Bundle extras = getIntent().getExtras();
		if(extras!=null)
		{
			OrderId = extras.getString("OrderId");
			ArrayList<OrderInfo> orderList = WebService.GetListNewestOrder(Session.currentSubAccount);
			for (OrderInfo orderTemp : orderList) {
				if(orderTemp.OrderId.equals(OrderId))
				{
					OrderInfo = orderTemp;
					Load_OrderInfo(OrderInfo);
					break;
				}
			}
		}
	}	

	private void Load_OrderInfo(OrderInfo order) {
		if(order!=null)
		{
			txtSymbol.setText(order.StockSymbol);
			txtPrice.setText(Float.toString(order.Price));
			txtVolume.setText(Integer.toString(order.Volume));
			txtDup.setText("1");
			
			strSymbol = txtSymbol.getText().toString();
            preTradeInfo = WebService.GetPreTradeInfo(Session.currentSubAccount, strSymbol, 0, "B");
			if(preTradeInfo!=null)
			{
				lbCeiling.setText(Float.toString(preTradeInfo.Ceiling));
				lbFloor.setText(Float.toString(preTradeInfo.Floor));
				lblRef.setText(Float.toString(preTradeInfo.RefPrice));
			}
			else
			{
				lbCeiling.setText("...");
				lbFloor.setText("...");
				lblRef.setText("...");
			}
			
			txtSymbol.setEnabled(false);
			txtVolume.setEnabled(false);
			txtDup.setEnabled(false);
		}
	}

	private void Load_Current_SubAccount_Info() {
		lbAccount.setText(Session.custAccount);
		CashBalance = WebService.GetCashBalance(Session.currentSubAccount, 0);
		if(CashBalance != Float.NaN)
			lbBalance.setText(Common.FormatNumber(CashBalance) + " VND");
	}

	private void CheckLogin()
	{
		if(!Session.IsLogin)
		{
			Intent intent = new Intent(getBaseContext(), DangNhapActivity.class);
			startActivity(intent);
			finish();
		}
	}
		
	private void Get_Input_Info() {
		//Get information
		strSymbol = txtSymbol.getText().toString();
		strVolume = txtVolume.getText().toString();
		strPrice = txtPrice.getText().toString();
		strDup = txtDup.getText().toString();
		strPIN = txtPIN.getText().toString();
	}
		
	private boolean Check_Order_Info() {
		preTradeInfo = WebService.GetPreTradeInfo(Session.currentSubAccount, strSymbol, 0, "B");
		
		//Check empty symbol
		if(strSymbol.equals(""))
		{
			Log.d("TAG", getResources().getString(R.string.str_empty_stocksymbol));
			//lbMessage.setText(getResources().getString(R.string.str_empty_stocksymbol));
			Toast.makeText(MuaActivity.this, R.string.str_empty_stocksymbol, Toast.LENGTH_SHORT).show();
			return false;
		}
						
		if(preTradeInfo==null)
		{
			Log.d("TAG", getResources().getString(R.string.str_invalid_stocksymbol));
			//lbMessage.setText(getResources().getString(R.string.str_invalid_stocksymbol));
			Toast.makeText(MuaActivity.this, R.string.str_invalid_stocksymbol, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Get market status
		marketStatus = WebService.GetAllMarketStatus();
		
		//Check market status-----
		if(marketStatus[preTradeInfo.MarketId - 1] == "U")
		{
			Log.d("TAG", getResources().getString(R.string.str_not_available_service));
			//lbMessage.setText(getResources().getString(R.string.str_not_available_service));
			Toast.makeText(MuaActivity.this, R.string.str_not_available_service, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Check save PIN----------
		//------------------------
		//------------------------
		//------------------------
		
		//Check volume------------
		
		//Check empty volume
		if(strSymbol.equals(""))
		{
			Log.d("TAG", getResources().getString(R.string.str_empty_volume));
			//lbMessage.setText(getResources().getString(R.string.str_empty_volume));
			Toast.makeText(MuaActivity.this, R.string.str_empty_volume, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Check numeric volume
		try
		{
			volume = Integer.parseInt(strVolume);
			if(volume < 0)
			{
				Log.d("TAG", getResources().getString(R.string.str_volume_bellow_zero));
				//lbMessage.setText(getResources().getString(R.string.str_volume_bellow_zero));
				Toast.makeText(MuaActivity.this, R.string.str_volume_bellow_zero, Toast.LENGTH_SHORT).show();
				return false;
			}
		}
		catch(Exception ex)
		{
			Log.d("TAG", getResources().getString(R.string.str_invalid_volume));
			//lbMessage.setText(getResources().getString(R.string.str_invalid_volume));
			Toast.makeText(MuaActivity.this, R.string.str_invalid_volume, Toast.LENGTH_SHORT).show();
			return false;
		}

		//Validate volume belong to market
		switch(preTradeInfo.MarketId)
		{
			case 1:
				if((volume % 10) != 0)
				{
					Log.d("TAG", getResources().getString(R.string.str_invalid_volume2));
					//lbMessage.setText(getResources().getString(R.string.str_invalid_volume2));
					Toast.makeText(MuaActivity.this, R.string.str_invalid_volume2, Toast.LENGTH_SHORT).show();
					return false;
				}

				if(volume > 19990)
				{
					Log.d("TAG", getResources().getString(R.string.str_invalid_volume3));
					//lbMessage.setText(getResources().getString(R.string.str_invalid_volume3));
					Toast.makeText(MuaActivity.this, R.string.str_invalid_volume3, Toast.LENGTH_SHORT).show();
					return false;
				}	
				break;			
			default:
				if((volume % 100) != 0)
				{
					Log.d("TAG", getResources().getString(R.string.str_invalid_volume2));
					//lbMessage.setText(getResources().getString(R.string.str_invalid_volume2));
					Toast.makeText(MuaActivity.this, R.string.str_invalid_volume2, Toast.LENGTH_SHORT).show();
					return false;
				}

				if(volume > 99900)
				{
					Log.d("TAG", getResources().getString(R.string.str_invalid_volume3));
					//lbMessage.setText(getResources().getString(R.string.str_invalid_volume3));
					Toast.makeText(MuaActivity.this, R.string.str_invalid_volume3, Toast.LENGTH_SHORT).show();
					return false;
				}
				break;
		}

		//Check price

		//Check empty price
		if(strPrice.equals(""))
		{
			Log.d("TAG", getResources().getString(R.string.str_empty_price));
			//lbMessage.setText(getResources().getString(R.string.str_empty_price));
			Toast.makeText(MuaActivity.this, R.string.str_empty_price, Toast.LENGTH_SHORT).show();
			return false;
		}											
		
		//Check numeric price or ATO/ATC price
		try
		{
			if(!(strPrice.equals("ATO") || strPrice.equals("ATC")))
			{
				price = Float.parseFloat(strPrice); 
			}
			else
			{
				//Set price type
				if( strPrice.equals("ATO"))
					conPrice = "A";
				else
					conPrice = "C";
				
				//Check marketstatus for ATO/ATC trade
				
				if(preTradeInfo.MarketId ==2)
				{
					Log.d("TAG", getResources().getString(R.string.str_not_allow_price_HNX));
					//lbMessage.setText(getResources().getString(R.string.str_not_allow_price_HNX));
					Toast.makeText(MuaActivity.this, R.string.str_not_allow_price_HNX, Toast.LENGTH_SHORT).show();
					return false;
				}
				
				if(preTradeInfo.MarketId ==3)
				{
					Log.d("TAG", getResources().getString(R.string.str_not_allow_price_UPCOM));
					//lbMessage.setText(getResources().getString(R.string.str_not_allow_price_UPCOM));
					Toast.makeText(MuaActivity.this, R.string.str_not_allow_price_UPCOM, Toast.LENGTH_SHORT).show();
					return false;
				}
				
				if( strPrice.equals("ATO") && ( marketStatus[0] != "S" || marketStatus[0] != "P" ))
				{
					Log.d("TAG", getResources().getString(R.string.str_ato_price_not_allow_current_phase));
					//lbMessage.setText(getResources().getString(R.string.str_ato_atc_price_not_allow_current_phase));
					Toast.makeText(MuaActivity.this, R.string.str_ato_price_not_allow_current_phase, Toast.LENGTH_SHORT).show();
					return false;
				}
				
				if( strPrice.equals("ATC") && marketStatus[0] != "A" )
				{
					Log.d("TAG", getResources().getString(R.string.str_atc_price_not_allow_current_phase));
					//lbMessage.setText(getResources().getString(R.string.str_ato_atc_price_not_allow_current_phase));
					Toast.makeText(MuaActivity.this, R.string.str_atc_price_not_allow_current_phase, Toast.LENGTH_SHORT).show();
					return false;
				}
				
				//If ATO/ATC price = ceiling
				price = preTradeInfo.Ceiling;
			}
		}
		catch(Exception ex)
		{
			Log.d("TAG", getResources().getString(R.string.str_not_valid_price));
			//lbMessage.setText(getResources().getString(R.string.str_not_valid_price));
			Toast.makeText(MuaActivity.this, R.string.str_not_valid_price, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Check if price <0				
		if(price < 0)
		{
			Log.d("TAG", getResources().getString(R.string.str_price_bellow_zero));
			//lbMessage.setText(getResources().getString(R.string.str_price_bellow_zero));
			Toast.makeText(MuaActivity.this, R.string.str_price_bellow_zero, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Check floor =< price =< ceiling 
		if(price > preTradeInfo.Ceiling || price < preTradeInfo.Floor)
		{
			Log.d("TAG", getResources().getString(R.string.str_price_not_bettwen_ceiling_floor));
			//lbMessage.setText(getResources().getString(R.string.str_price_not_bettwen_ceiling_floor));
			Toast.makeText(MuaActivity.this, R.string.str_price_not_bettwen_ceiling_floor, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Check multiple price
		if(preTradeInfo.MarketId==1)
		{
			float dbCurrentPrice = price*1000;
            if (dbCurrentPrice < 50000 && dbCurrentPrice%100 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_100));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_100));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_100, Toast.LENGTH_SHORT).show();
				return false;
            }
            else if (dbCurrentPrice >= 50000 && dbCurrentPrice < 10000 && dbCurrentPrice%500 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_500));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_500));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_500, Toast.LENGTH_SHORT).show();
				return false;
            }
            else if (dbCurrentPrice >= 100000 && dbCurrentPrice%1000 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_1000));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_1000));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_1000, Toast.LENGTH_SHORT).show();
				return false;
            }
		}
		else
		{
			float dbCurrentPrice = price * 1000;
            if (dbCurrentPrice % 100 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_100));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_100));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_100, Toast.LENGTH_SHORT).show();
				return false;
            }
		}
		
		//Check dup
		try
		{
			dup = Integer.parseInt(strDup);
			if(dup < 1 || dup > 9)
			{
				Log.d("TAG", getResources().getString(R.string.str_dup_invalid));
				//lbMessage.setText(getResources().getString(R.string.str_dup_invalid));
				Toast.makeText(MuaActivity.this, R.string.str_dup_invalid, Toast.LENGTH_SHORT).show();
				return false;
			}
		}
		catch(Exception ex)
		{
			Log.d("TAG", getResources().getString(R.string.str_dup_not_numeric));
			//lbMessage.setText(getResources().getString(R.string.str_dup_not_numeric));
			Toast.makeText(MuaActivity.this, R.string.str_dup_not_numeric, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//Check empty PIN
		if(strPIN.equals(""))
		{
			Log.d("TAG", getResources().getString(R.string.str_empty_pin));
			//lbMessage.setText(getResources().getString(R.string.str_empty_pin));
			Toast.makeText(MuaActivity.this, R.string.str_empty_pin, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		CashBalance = WebService.GetCashBalance(Session.currentSubAccount, 0);
		if(CashBalance == Float.NaN)
		{
			Log.d("TAG", getResources().getString(R.string.str_can_not_determine_cashbalance));
			//lbMessage.setText(getResources().getString(R.string.str_can_not_determine_cashbalance));
			Toast.makeText(MuaActivity.this, R.string.str_can_not_determine_cashbalance, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		float money = volume * price * 1000 * dup * WebService.GetFee();
		
		if(money > CashBalance || money > preTradeInfo.AvailableCash)
		{
			Log.d("TAG", getResources().getString(R.string.str_not_enough_money));
			//lbMessage.setText(getResources().getString(R.string.str_not_enough_money));
			Toast.makeText(MuaActivity.this, R.string.str_not_enough_money, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		return true;
	}
	
	private void PutOrder() {
		int count = 0;
		for(int i = 0; i < dup; i++)
		{
			int putorderRetCode = WebService.PutOrder(preTradeInfo.MarketId, Session.currentSubAccount, strPIN, strSymbol, "B", volume, price, conPrice, 0, "W");
			
			if(putorderRetCode!=RET_CODE.SUCCESS.Value)
			{
				Log.d("TAG", getResources().getString(R.string.str_order_not_success));
				//lbMessage.setText(getResources().getString(R.string.str_order_not_success));
				Toast.makeText(MuaActivity.this, R.string.str_order_not_success, Toast.LENGTH_SHORT).show();
				return;
			}
			else
			{
				count++;
			}
		}
		
		if(count == dup)
		{
			txtSymbol.setText("");
			txtPrice.setText("");
			txtVolume.setText("");
			txtPIN.setText("");
			txtDup.setText("");
			//lbMessage.setText(R.string.str_order_success + "!");
		}
		else
		{
			Log.d("TAG", getResources().getString(R.string.str_order_success) + count + "/" + dup + getResources().getString(R.string.str_order_ordered));
			//lbMessage.setText(getResources().getString(R.string.str_order_success) + count + "/" + dup + getResources().getString(R.string.str_order_ordered));
			Toast.makeText(MuaActivity.this,getResources().getString(R.string.str_order_success) + count + "/" + dup + getResources().getString(R.string.str_order_ordered), Toast.LENGTH_SHORT).show();
			return;
		}
	}
	
	private void UpdateOrder() {
		strPrice = txtPrice.getText().toString();
		strPIN = txtPIN.getText().toString();
		
		//Get market status
		marketStatus = WebService.GetAllMarketStatus();
		
		//Get stock info
		preTradeInfo = WebService.GetPreTradeInfo(Session.currentSubAccount, strSymbol, 0, "B");
		
		if(preTradeInfo==null)
		{
			Log.d("TAG", getResources().getString(R.string.str_invalid_stocksymbol));
			//lbMessage.setText(getResources().getString(R.string.str_invalid_stocksymbol));
			Toast.makeText(MuaActivity.this, R.string.str_invalid_stocksymbol, Toast.LENGTH_SHORT).show();
			return;
		}
		
		CashBalance = WebService.GetCashBalance(Session.currentSubAccount, 0);
		if(CashBalance == Float.NaN)
		{
			Log.d("TAG", getResources().getString(R.string.str_can_not_determine_cashbalance));
			//lbMessage.setText(getResources().getString(R.string.str_can_not_determine_cashbalance));
			Toast.makeText(MuaActivity.this, R.string.str_can_not_determine_cashbalance, Toast.LENGTH_SHORT).show();
			return;
		}
		
		//Check empty price
		if(strPrice.equals(""))
		{
			Log.d("TAG", getResources().getString(R.string.str_empty_price));
			//lbMessage.setText(getResources().getString(R.string.str_empty_price));
			Toast.makeText(MuaActivity.this, R.string.str_empty_price, Toast.LENGTH_SHORT).show();
			return;
		}
		
		//Check numeric price or ATO/ATC price
		try
		{
			if(!(strPrice.equals("ATO") || strPrice.equals("ATC")))
			{
				price = Float.parseFloat(strPrice); 
			}
			else
			{				
				Log.d("TAG", getResources().getString(R.string.str_not_allow_ato_atc_price));
				//lbMessage.setText(getResources().getString(R.string.str_not_allow_ato_atc_price));
				Toast.makeText(MuaActivity.this, R.string.str_not_allow_ato_atc_price, Toast.LENGTH_SHORT).show();
				return;
			}
		}
		catch(Exception ex)
		{
			Log.d("TAG", getResources().getString(R.string.str_not_valid_price));
			//lbMessage.setText(getResources().getString(R.string.str_not_valid_price));
			Toast.makeText(MuaActivity.this, R.string.str_not_valid_price, Toast.LENGTH_SHORT).show();
			return;
		}
		
		//Check if price <0
		if(price < 0)
		{
			Log.d("TAG", getResources().getString(R.string.str_price_bellow_zero));
			//lbMessage.setText(getResources().getString(R.string.str_price_bellow_zero));
			Toast.makeText(MuaActivity.this, R.string.str_price_bellow_zero, Toast.LENGTH_SHORT).show();
			return;
		}
		
		//Check floor =< price =< ceiling 
		if(price > preTradeInfo.Ceiling || price < preTradeInfo.Floor)
		{
			Log.d("TAG", getResources().getString(R.string.str_price_not_bettwen_ceiling_floor));
			//lbMessage.setText(getResources().getString(R.string.str_price_not_bettwen_ceiling_floor));
			Toast.makeText(MuaActivity.this, R.string.str_price_not_bettwen_ceiling_floor, Toast.LENGTH_SHORT).show();
			return;
		}
		
		//Check multiple price
		if(preTradeInfo.MarketId==1)
		{
			float dbCurrentPrice = price*1000;
            if (dbCurrentPrice < 50000 && dbCurrentPrice%100 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_100));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_100));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_100, Toast.LENGTH_SHORT).show();
				return;
            }
            else if (dbCurrentPrice >= 50000 && dbCurrentPrice < 10000 && dbCurrentPrice%500 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_500));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_500));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_500, Toast.LENGTH_SHORT).show();
				return;
            }
            else if (dbCurrentPrice >= 100000 && dbCurrentPrice%1000 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_1000));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_1000));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_1000, Toast.LENGTH_SHORT).show();
				return;
            }
		}
		else
		{
			float dbCurrentPrice = price * 1000;
            if (dbCurrentPrice % 100 != 0)
            {
            	Log.d("TAG", getResources().getString(R.string.str_pricestep_invalid_100));
				//lbMessage.setText(getResources().getString(R.string.str_pricestep_invalid_100));
				Toast.makeText(MuaActivity.this, R.string.str_pricestep_invalid_100, Toast.LENGTH_SHORT).show();
				return;
            }
		}
		
		//Check empty PIN
		if(strPIN.equals(""))
		{
			Log.d("TAG", getResources().getString(R.string.str_empty_pin));
			//lbMessage.setText(getResources().getString(R.string.str_empty_pin));
			Toast.makeText(MuaActivity.this, R.string.str_empty_pin, Toast.LENGTH_SHORT).show();
			return;
		}
		
		float money = volume * price * 1000 * WebService.GetFee();
		
		if(money > CashBalance || money > preTradeInfo.AvailableCash)
		{
			Log.d("TAG", getResources().getString(R.string.str_not_enough_money));
			//lbMessage.setText(getResources().getString(R.string.str_not_enough_money));
			Toast.makeText(MuaActivity.this, R.string.str_not_enough_money, Toast.LENGTH_SHORT).show();
			return;
		}
				
		int update_order_retcode = WebService.Update_Order(Session.currentSubAccount, strPIN, 0, OrderInfo.OrderId, price);

		if(update_order_retcode !=RET_CODE.SUCCESS.Value)
		{
			Log.d("TAG", getResources().getString(R.string.str_order_not_success));
			//lbMessage.setText(getResources().getString(R.string.str_order_not_success));
			Toast.makeText(MuaActivity.this, R.string.str_order_not_success, Toast.LENGTH_SHORT).show();
			return;
		}
		else
		{
			Log.d("TAG", getResources().getString(R.string.str_order_success));
			//lbMessage.setText(getResources().getString(R.string.str_order_success));
			Toast.makeText(MuaActivity.this, R.string.str_order_success, Toast.LENGTH_SHORT).show();
			return;
		}
	}
	
	@Override
	protected void onResume()
	{
		CheckLogin();
		super.onResume();
	}
}